#!/bin/sh
#
PREFIX=/usr/cross
PATH=${PREFIX}/bin:${PATH}
TARGET=i586-elf
FETCH=1

if [ -f ./common_funcs.sh ]; then
    . ./common_funcs.sh
else
    echo "Cannot function without common_funcs.sh!" && exit 3
fi

function inst_gcc() {
    if [ ${FETCH} -gt 0 ]; then
        bak_dir gcc-latest

        svn checkout svn://gcc.gnu.org/svn/gcc/trunk gcc-latest
    fi

    inst_binutils || exit 1

    inst_gmp || exit 1

    inst_mpfr || exit 1

    if [ ! ${FETCH} -eq 2 ]; then
        (cd ../build-gcc 2>/dev/null && \
        ../gcc-latest/configure --target=${TARGET} --prefix=${PREFIX} --disable-nls --enable-languages=c,c++ --without-headers && \
        make all-gcc && make check && sudo make install-gcc) || (echo "gcc build failed!"; return 1)
    fi
}

function inst_binutils() {
	local binutils_ver="binutils-2.20"

    if [ ${FETCH} -gt 0 ]; then
		local binutils_url="http://ftp.gnu.org/gnu/binutils/${binutils_ver}.tar.gz"
		
		if chk_cmd wget; then
			wget $binutils_url
		elif chk_cmd curl ; then
			curl -C - -O $binutils_url
		else
			echo "You must have either curl or wget to use this script!"
			return 2
		fi
	
        bak_dir build-binutils
        bak_dir build-gcc
		bak_dir ${binutils_ver}
		
        mkdir build-binutils build-gcc
		
		tar -xzf ${binutils_ver}.tar.gz
    fi

    if [ ! ${FETCH} -eq 2 ]; then
        ((cd build-binutils 2>/dev/null && \
        ../${binutils_ver}/configure --target=${TARGET} --prefix=${PREFIX} --disable-nls && \
        make all) || ((rmv_word build-binutils/binutils/Makefile -Werror) && (cd build-binutils 2>/dev/null && make all && make check && sudo make install))) || (echo "binutils build failed!"; return 1)
    fi
}

function inst_gmp() {
    local gmp_ver="gmp-5.0.1"

    if [ ${FETCH} -gt 0 ]; then
        local gmp_url="ftp://ftp.gmplib.org/pub/${gmp_ver}/${gmp_ver}.tar.bz2"

        if [ ! -f ${gmp_ver}.tar.bz2 ]; then
            if chk_cmd curl; then
                curl -C - -O $gmp_url
            elif chk_cmd wget; then
                wget $gmp_url
            else
                echo "You must either have curl or wget to run this script!\n"
                exit 2
            fi
        fi

        bak_dir ${gmp_ver}

        tar xjf ${gmp_ver}.tar.bz2 # && rm ${gmp_ver}.tar.bz2
    fi

    if [ ! ${FETCH} -eq 2 ]; then
        (cd ${gmp_ver} 2>/dev/null && make all && sudo make install) || (echo "gmp build failed!" && return 1)
    fi
}

function inst_mpfr() {
    if [ ${FETCH} -gt 0 ]; then
        svn checkout svn://scm.gforge.inria.fr/svn/mpfr/trunk mpfr
    fi

    if [ ! ${FETCH} -eq 2 ]; then
        (cd mpfr 2>/dev/null && make all && sudo make install) || (echo "mpfr build failed!" && return 1)
    fi
}

case ${1} in
    fetch_binutils)
        FETCH=2
        inst_binutils || exit 1
        ;;
    binutils)
        inst_binutils || exit 1
        ;;
    fetch_gmp)
        FETCH=2
        inst_gmp || exit 1
        ;;
    gmp)
        inst_gmp || exit 1
        ;;
    fetch_mpfr)
        FETCH=2
        inst_mpfr || exit 1
        ;;
    mpfr)
        inst_mpfr || exit 1
        ;;
    fetch)
        FETCH=2
        inst_gcc || exit 1
        ;;
    nofetch)
        FETCH=0
        inst_gcc || exit 1
        ;;
    *)
        inst_gcc || exit 1
        ;;
esac

# EOF
